package com.example.vp2loadmoreview.banner;

import android.os.Bundle;

import com.example.vp2loadmoreview.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

//循环左or右滑动
public class ViewPager2Activity extends AppCompatActivity {
    private ViewPager2 mViewPager2;
    private Vp2Adapter mAdapter;
    private int selectPos;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager2);
        mViewPager2 = findViewById(R.id.vp2);
        List<String> mList = new ArrayList<>();
        for (int i = 0; i < 3; i++){
            mList.add("item" + i);
        }
        mAdapter = new Vp2Adapter(this,mList);
        mViewPager2.setAdapter(mAdapter);
        mViewPager2.setPageTransformer(new CustomPageTransformer());
        mViewPager2.setCurrentItem(0);
        mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                selectPos = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                if (state == ViewPager2.SCROLL_STATE_IDLE) {
                    if (selectPos == mAdapter.getItemCount() - 1) {
                        mViewPager2.setCurrentItem(1, false);
                    }
                    else if (selectPos == 0) {
                        mViewPager2.setCurrentItem(mAdapter.getItemCount() - 2, false);
                    }

                }
            }
        });
    }
}
